Test a REST API end to end
Test an API that needs a login: sign in once, keep the token, then call a protected endpoint with it and check the result.
Before you start.
- Permissions: Build Collection, Update Test Case, Publish Collection, Run Test (Permissions).
- A REST API the AGIS server can reach, with a test user.
- If your API simply uses Basic, Bearer, API key or OAuth2 client-credentials authentication, you do not need the login call in this guide. Set that Auth type on the environment and skip to step 4.
Steps
-
Store the password as a secret. In Secrets, click + Add secret and save the test user's password, for example as
api_password. See Secrets. -
Create an environment. In Environments, create a REST / SOAP environment with the API's Base URL and Auth = None. See Environments.
-
Create a collection. Click + New API Test Collection, pick that environment as the Default environment · REST / SOAP and create it. See Collections and cases.
-
Declare a run variable for the token. Open the collection's Variables tab, click Declare Variables, enter the name
token(without therun.prefix) and click Save variables. You will use it as{{run.token}}. See Variables tab. -
Add the login case. In the Cases tab, click Add Case, choose the method POST and the login path. On the case's Request tab, set Body Type to JSON and enter the body, using the secret for the password:
{"username": "test-user", "password": "{{secret.api_password}}"} -
Extract the token. On the Extract tab, click Add extraction and set:
Field Value From BODY_JSONPATHPath the path of the token in the response, for example $.tokenInto Variable run.tokenIf Not Found Block Dependants Turn on Mask stored value so the token is hidden in results. See Extract.
-
Check the login. On the Assertions tab, keep the STATUS
EQUALS200rule (use the status your API returns for a successful login). See Assertions. -
Add the protected call. Add a second case with the method and path of the endpoint you want to test. On its Request tab, click Add Header and add:
Key Value Authorization Bearer {{run.token}}The case must come after the login case in the list. Drag rows to change the order.
-
Add checks. On the second case's Assertions tab, check the status and at least one value in the body, for example
BODY_JSONPATH$.idEXISTS. -
Publish and run. Click Publish in the collection header. Fix anything listed under "Fix or review this case before publishing". Then open the Run tab, check the Environment and click Start run. See Running tests.
Check the result
Open History and click the run. Both cases should be Passed. If the login fails, the second case shows Blocked, because the token could not be extracted.
Common problems
| What you see | Why | What to do |
|---|---|---|
| Second case is Blocked | The token was not extracted, or the variable is not declared | Check the Extract Path, and that token is declared on the Variables tab. |
| Publish fails with undeclared references | {{run.token}} is used but not declared | Declare it (step 4). |
SECRET_MISSING | The secret does not exist in the vault of the person who starts the run | Add the secret with the same name. See Secrets. |
| Connection errors on every case | The AGIS server cannot reach the Base URL | Use an address the server can reach, not localhost on your computer. |